/*
 * Copyright (C) 2016-2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.VectorElement;
import ohos.agp.window.dialog.CommonDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.sample.utils.NumUtil;

import java.io.IOException;

/**
 * DialogStyleAbility
 *
 * @since 2021-06-21
 */
public class DialogStyleAbility extends CommonDialog implements Component.ClickedListener {
    private Ability mAbility;
    private DependentLayout mDlMenu;
    private ComponentContainer mRootView;
    private boolean isMenuShow = false;
    private Component dialogBg;
    private DependentLayout dialogView;
    private MaterialTapTargetPrompt tapTargetPrompt;

    /**
     * DialogStyleAbility
     *
     * @param ability
     */
    public DialogStyleAbility(Ability ability) {
        super(ability.getContext());
        mAbility = ability;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setAutoClosable(false);
        setTransparent(true);
        Component contentView = LayoutScatter.getInstance(mAbility)
                .parse(ResourceTable.Layout_ability_dialog_style, null, false);
        setContentCustomComponent(contentView);
        initViews();
    }

    private void initViews() {
        dialogView = (DependentLayout) getContentCustomComponent().findComponentById(ResourceTable.Id_dialog_view);
        dialogView.setClickedListener(component -> {
            if (tapTargetPrompt != null) {
                tapTargetPrompt.dismiss();
            }
        });
        mRootView = (ComponentContainer) getContentCustomComponent().findComponentById(ResourceTable.Id_dl_dialog);
        getContentCustomComponent().findComponentById(ResourceTable.Id_ig_left).setClickedListener(this);
        getContentCustomComponent().findComponentById(ResourceTable.Id_ig_dialog).setClickedListener(this);
        mDlMenu = (DependentLayout) getContentCustomComponent().findComponentById(ResourceTable.Id_dl_menu);
        dialogBg = getContentCustomComponent().findComponentById(ResourceTable.Id_dialog_bg);
        dialogBg.setClickedListener(component -> mDlMenu.callOnClick());
        mDlMenu.setScaleX(0);
        mDlMenu.setPivot(mDlMenu.getWidth(), 0);
        mDlMenu.setClickedListener(this);
        getContentCustomComponent().findComponentById(ResourceTable.Id_btn_navigation_prompt).setClickedListener(this);
        getContentCustomComponent().findComponentById(ResourceTable.Id_showOverflowPrompt).setClickedListener(this);
        getContentCustomComponent().findComponentById(ResourceTable.Id_showSearchPrompt).setClickedListener(this);
        getContentCustomComponent().findComponentById(ResourceTable.Id_button_fab_prompt).setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_navigation_prompt:
                showSideNavigationPrompt();
                break;
            case ResourceTable.Id_showOverflowPrompt:
                showOverflowPrompt();
                break;
            case ResourceTable.Id_showSearchPrompt:
                showSearchPrompt();
                break;
            case ResourceTable.Id_button_fab_prompt:
                showFabPrompt();
                break;
            case ResourceTable.Id_ig_left:
                hide();
                break;
            case ResourceTable.Id_ig_dialog:
                showMenu();
                break;
            case ResourceTable.Id_dl_menu:
                if (isMenuShow) {
                    isMenuShow = false;
                    mDlMenu.setScaleX(0);
                    dialogBg.setVisibility(Component.HIDE);
                }
                break;
            default:
                break;
        }
    }

    private void showMenu() {
        AnimatorProperty animatorProperty = mDlMenu.createAnimatorProperty();
        animatorProperty.setDuration(NumUtil.INT_100);
        if (!animatorProperty.isRunning()) {
            if (isMenuShow) {
                animatorProperty.scaleX(0);
                dialogBg.setVisibility(Component.HIDE);
            } else {
                animatorProperty.scaleX(1);
                dialogBg.setVisibility(Component.VISIBLE);
            }
            isMenuShow = !isMenuShow;
            animatorProperty.start();
        }
    }

    /**
     * showFabPrompt
     */
    public void showFabPrompt() {
        tapTargetPrompt = new MaterialTapTargetPrompt.Builder(mAbility, mRootView)
                .setTarget(getContentCustomComponent().findComponentById(ResourceTable.Id_fab))
                .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                .setPrimaryText("Clipped to ability bounds")
                .setSecondaryText("The prompt does not draw outside the ability")
                .setClipToView(mRootView)
                .show();
    }

    /**
     * showSideNavigationPrompt
     */
    public void showSideNavigationPrompt() {
        try {
            tapTargetPrompt = new MaterialTapTargetPrompt.Builder(mAbility, mRootView)
                    .setPrimaryText(ResourceTable.String_menu_prompt_title)
                    .setSecondaryText(ResourceTable.String_menu_prompt_description)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setMaxTextWidth(ResourceTable.Float_tap_target_menu_max_width)
                    .setIcon(new VectorElement(mAbility, ResourceTable.Graphic_ic_back))
                    .setTarget(getContentCustomComponent().findComponentById(ResourceTable.Id_ig_left))
                    .setClipToView(mRootView)
                    .show();
        } catch (NotExistException | IOException | WrongTypeException e) {
            String error = e.toString();
        }
    }

    /**
     * showOverflowPrompt
     */
    public void showOverflowPrompt() {
        try {
            tapTargetPrompt = new MaterialTapTargetPrompt.Builder(mAbility, mRootView)
                    .setPrimaryText(ResourceTable.String_overflow_prompt_title)
                    .setSecondaryText(ResourceTable.String_overflow_prompt_description)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setMaxTextWidth(ResourceTable.Float_tap_target_menu_max_width)
                    .setIcon(new VectorElement(mAbility, ResourceTable.Graphic_ic_more_vert))
                    .setTarget(getContentCustomComponent().findComponentById(ResourceTable.Id_ig_dialog))
                    .setClipToView(mRootView)
                    .show();
        } catch (NotExistException | WrongTypeException | IOException e) {
            String error = e.toString();
        }
    }

    /**
     * showSearchPrompt
     */
    public void showSearchPrompt() {
        try {
            tapTargetPrompt = new MaterialTapTargetPrompt.Builder(mAbility, mRootView)
                    .setPrimaryText(ResourceTable.String_search_prompt_title)
                    .setSecondaryText(ResourceTable.String_search_prompt_description)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setMaxTextWidth(ResourceTable.Float_max_prompt_width)
                    .setIcon(new VectorElement(mAbility, ResourceTable.Graphic_ic_search))
                    .setTarget(getContentCustomComponent().findComponentById(ResourceTable.Id_ig_search))
                    .setClipToView(mRootView)
                    .show();
        } catch (NotExistException | WrongTypeException | IOException e) {
            String error = e.toString();
        }
    }
}
